package com.education.wisdom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.wisdom.pojo.JrCourse;
import com.education.wisdom.pojo.MsMessage;
import com.education.wisdom.pojo.TeacherClass;
import com.education.wisdom.pojo.Teachercourse;
import com.education.wisdom.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.education.common.core.BaseController.writeJSON;


@Controller
@CrossOrigin
@Slf4j
@ResponseBody
@Api(tags = {"老师端其他功能"})
@RequestMapping("/web/teacherAll")
public class FullTeacherAllController {

    @Autowired
    private IJrCourseService jrCourseService;

    @Autowired
    private MsMessageService messageService;

    @Autowired
    private IStudentClassService studentClassService;

    @Autowired
    private ITeachercourseService teachercourseService;

    @Autowired
    private ITeacherClassService teacherClassService;

    @GetMapping("/classSchedule")
    public void classSchedule(HttpServletResponse response, Integer classId) throws IOException {
        log.info(LogUtil.LogInfo("教师查询课程表", new Date()));
        List<JrCourse> jrCourses = jrCourseService.selectJrCourseByClassId(classId);
        writeJSON(jrCourses, response, "200", "success");
    }

    @PostMapping("/announce")
    public void announce(HttpServletResponse response, MsMessage message) throws IOException {
        log.info(LogUtil.LogInfo("教师发布公告", new Date()));
        if (message.getContent() != null) {
//            List<StudentClass> studentClass = classService.selectClassIdByUserId(message.getUserId());
//            for (StudentClass aClass : studentClass) {
//                message.setClassId(aClass.getClassId());
                message.setCreatetime(new Date());
                messageService.insertMessage(message);
//            }
            writeJSON("发布成功", response, "200", "success");
        } else {
            writeJSON("内容不能为空", response, "201", "success");
        }
    }

    @GetMapping("/viewAnnouncement")
    public void viewAnnouncement(HttpServletResponse response, String userId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("教师浏览公告记录", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<MsMessage> messages = messageService.selectByUserId(userId);
        writeJSON(new PageInfo<>(messages), response, "200", "success");
    }

    @GetMapping("/announcementDetails")
    public void announcementDetails(HttpServletResponse response, Integer id) throws IOException {
        log.info(LogUtil.LogInfo("教师端查询公告详情", new Date()));
        MsMessage msMessage = messageService.selectMessageById(id);
        writeJSON(msMessage, response, "200", "success");
    }

    @PostMapping("/uploadCourseware")
    public void uploadCourseware(HttpServletResponse response, Teachercourse teachercourse) throws IOException {
        log.info(LogUtil.LogInfo("上传课件", new Date()));
        if (teachercourse.getImgurl() != null) {
            TeacherClass teacherClass = teacherClassService.selectTeacherClassById(teachercourse.getUserId());
            teachercourse.setClassId(teacherClass.getClassId());
            teachercourse.setCreateTime(new Date());
            teachercourseService.insertTeachercourse(teachercourse);
            writeJSON("上传成功", response, "200", "success");
        }
        writeJSON("没有上传资源", response, "201", "success");
    }

    @GetMapping("/coursewareList")
    public void coursewareList(HttpServletResponse response,Teachercourse teachercourse, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("老师查询上传列表", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<Teachercourse> teachercourses = teachercourseService.selectTeachercourseList(teachercourse);
        writeJSON(new PageInfo<>(teachercourses), response, "200", "success");
    }
}